package com.cy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cy.model.Book;
import com.cy.service.IBookService;
import com.cy.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    IBookService bookService;

    @RequestMapping("/page")
    public JsonResult page(Integer currentPage, Integer pageSize, Book book) {
        IPage<Book> page = bookService.page(currentPage, pageSize, book);
        return JsonResult.ok(page);
    }

    @RequestMapping("/list")
    public JsonResult list() {
        List<Book> list = bookService.list();
        return JsonResult.ok(list);
    }

    @RequestMapping("/getById")
    public JsonResult getById(Integer id) {
        Book book = bookService.getById(id);
        return JsonResult.ok(book);
    }

    @RequestMapping("/save")
    public JsonResult save(Book book) {
        bookService.save(book);
        return JsonResult.ok();
    }

    @RequestMapping("/update")
    public JsonResult update(Book book) {
        bookService.updateById(book);
        return JsonResult.ok();
    }

    @RequestMapping("/delete")
    public JsonResult delete(Integer id) {
        bookService.removeById(id);
        return JsonResult.ok();
    }

}
